﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class EditBG : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

            protected void Page_Load(object sender, EventArgs e)
            {
                Page.Header.Controls.Add(new LiteralControl(@"<base target=""_self"" />"));
                
                user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);
            
            if (!ReportHelper.IsAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            } 

            else if (!IsPostBack)
                {
                    if (Request["id"] != null)
                        if (Request["id"] == "-1")
                        {
                            this.SaveButton.Text = "添加(Add)";
                            this.CNTitleLabel.Text = "添加BG";
                            this.ENTitleLabel.Text = "Add A BG";
                        }
                        else
                        {
                            this.SaveButton.Text = "保存(Save)";
                            this.CNTitleLabel.Text = "编辑BG信息";
                            this.ENTitleLabel.Text = "Edit BG Information";
                            this.LoadData();
                        }

                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                }
            }

            protected void LoadData()
            {
                try
                {
                    int BGID = int.Parse(Request["id"].ToString());

                    BEGDC.PreContract.BLL.IBG iBG = (BEGDC.PreContract.BLL.IBG)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BG_Service];
                    BEGDC.PreContract.DAL.BG bg = iBG.GetBG(BGID);

                    this.CodeTextBox.Text = bg.BGCode;
                    this.SortNumberTextBox.Text = bg.SortNumber.ToString();
                    this.ActiveCheckBox.Checked = bg.Active;

                    //Joan 2008-10-20 date label format
                    DateTime date;
                    if (bg.CreateDate != Nullables.NullableDateTime.Default)
                    {
                        date = System.Convert.ToDateTime(bg.CreateDate.Value);
                        this.CreateDateLabel.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
                    }

                    if (bg.UpdateDate != Nullables.NullableDateTime.Default)
                    {
                        date = System.Convert.ToDateTime(bg.UpdateDate.Value);
                        this.UpdateDateLabel.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
                    }
                    //this.CreateDateLabel.Text = bg.CreateDate.ToString();
                    //this.UpdateDateLabel.Text = bg.UpdateDate.ToString();
                    //end
                }

                catch (Exception ex)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
                }

            }

            protected void SaveButton_Click(object sender, EventArgs e)
            {
                try
                {
                    int BGID = int.Parse(Request["id"].ToString());
                    bool flag = true;

                    if (this.IsBGCodeExist(this.CodeTextBox.Text.Trim(), BGID))
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.ExistedRecord) + "')</script>");
                        return;
                    }

                    if (this.IsSortNumberExist(this.SortNumberTextBox.Text.Trim(), BGID))
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.HasSortNumber) + "')</script>");
                        return;
                    }
                        
                    if (BGID == -1)
                    {
                        BEGDC.PreContract.BLL.IBG iBG = (BEGDC.PreContract.BLL.IBG)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BG_Service];
                        int numflag = iBG.AddBG(this.CodeTextBox.Text.Trim(), Convert.ToInt32(this.SortNumberTextBox.Text.Trim()), this.ActiveCheckBox.Checked, DateTime.Now, DateTime.Now);
                        if (numflag == 0)
                            flag = false;
                    }
                    else if (this.HasBD(BGID) && !this.ActiveCheckBox.Checked)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.HasBD) + "')</script>");
                        return;
                    }
                    else
                    {
                        
                        BEGDC.PreContract.BLL.IBG iBG = (BEGDC.PreContract.BLL.IBG)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BG_Service];
                        BEGDC.PreContract.DAL.BG bg = iBG.GetBG(BGID);

                        flag = iBG.UpdateBG(BGID, this.CodeTextBox.Text.Trim(), Convert.ToInt32(this.SortNumberTextBox.Text.Trim()), this.ActiveCheckBox.Checked, bg.CreateDate, DateTime.Now);
                    }

                    if (!flag)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.ExistedRecord) + "')</script>");
                        return;
                    }

                    this.Response.Write("<script language='javascript'>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess) + "');var arr=new Array(); arr[0]=0; window.returnValue=arr;window.close();</script>");

                }
                catch (Exception ex)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
                }
            }

        protected bool IsBGCodeExist(string bgCode, int bgID)
        {
            string condition = String.Format("BGCode = '{0}'",bgCode);

            if (bgID != -1)
                condition += String.Format(" AND BGID <> {0}",bgID);

            BEGDC.PreContract.BLL.IBG iBG = (BEGDC.PreContract.BLL.IBG)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BG_Service];

            DataTable dt = iBG.GetBG_DataTable(condition);

            if (dt.Rows.Count == 0)
                return false;
            else
                return true;
        }

        protected bool IsSortNumberExist(string sortNumber,int bgID)
        {
            string condition = String.Format("SortNumber = '{0}'",sortNumber);

            if (bgID != -1)
                condition += String.Format(" AND BGID <> {0}", bgID);

            BEGDC.PreContract.BLL.IBG iBG = (BEGDC.PreContract.BLL.IBG)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BG_Service];

            DataTable dt = iBG.GetBG_DataTable(condition);

            if (dt.Rows.Count == 0)
                return false;
            else
                return true;
        }

        protected bool HasBD(int bgID)
        {
            string condition = String.Format("BGID = {0}",bgID);
            
            BEGDC.PreContract.BLL.IBD iBD = (BEGDC.PreContract.BLL.IBD)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_BD_Service];
            DataTable dt = iBD.GetBD_DataTable(condition);

            if (dt.Rows.Count == 0)
                return false;
            else
                return true;
        }
    }
}

